While serving objects we use toResource method to transform them into resources and on the way back (posting a resource representation from client to server) how can I transform the representation back to the domain object?
I want to construct Book(@Entity) class from BookResource(extends ResourceSupport) class.
@RequestMapping(path="/", method = RequestMethod.POST, produces="application/vnd.company.app.book-v1+hal+json")
public ResponseEntity<?> addBook(@RequestBody BookResource bookResource) {
//What to do here?
}
Your BookResource should extend Resource rather than ResourceSupport.
public class BookResource extends Resource<Book> {
public BookResource(Book content, Link... links) {
super(content, links);
}
}
That way, you get the getContent() method for free, which "Returns the underlying entity."